package com.teddy.customWidgets;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import com.hp.hpl.jena.rdf.model.Property;
import com.teddy.controller.RDFSchemaControl;
import com.teddy.util.FileManager;

public abstract class BasePropertiesGroup extends Group{

	public Property selectedProperty;
	public BasePropertiesGroup(Composite parent, int style) {
		super(parent, style);
		// TODO Auto-generated constructor stub
	}

	protected void createPropertiesSingleValue( Composite parent, 
												String labelKey, 
												String value, 
												boolean isMultiLine
											) {
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
	
		Text editBox;
		GridData gridData;
		if( isMultiLine ) {
			editBox = new Text( propertyComposite, SWT.WRAP | SWT.MULTI | SWT.NONE );
			gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
			gridData.minimumHeight = 50;
			editBox.setLayoutData( gridData );
		} else {
			editBox = new Text( propertyComposite, SWT.NONE );
			editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		}
		if (value != null)
			editBox.setText(value);
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
	}
	
	protected Text createPropertiesSingleValue2( Composite parent, 
			String labelKey, 
			String value, 
			boolean isMultiLine ) {
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		Text editBox;
		GridData gridData;
		if( isMultiLine ) {
			editBox = new Text( propertyComposite, SWT.WRAP | SWT.MULTI | SWT.NONE );
			gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
			gridData.minimumHeight = 50;
			editBox.setLayoutData( gridData );
		} else {
			editBox = new Text( propertyComposite, SWT.NONE );
			editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		}
		if(value != null)
			editBox.setText(value);
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		editBox.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				// TODO Auto-generated method stub
				e.data.toString();
			}
		});
		
		return editBox;
	}
	
	protected Link createSingleValueLinkProperty( Composite parent, 
												  String labelKey, 
												  String value ) {
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		Link editBox;
		editBox = new Link( propertyComposite, SWT.NONE );
		editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		if(value != null)
			editBox.setText(value);
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		return editBox;
	}
	
	protected ExpandItem createProperties2ColumnMultipleValue( ExpandBar bar,
  													     String labelKey,
														 int propertyID,
														 List<String> values, 
														 int index,
														 boolean isResource) {
		
		Composite composite = new Composite (bar, SWT.NONE);
		GridLayout layout = new GridLayout (2, false);
		layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
		layout.verticalSpacing = 10;
		composite.setLayout(layout);
		
		refreshExpandItem( composite, propertyID, values, isResource);
		
		ExpandItem item = new ExpandItem (bar, SWT.NONE, index);
		item.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		item.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
		item.setControl(composite);
		item.setExpanded(true);
		return item;
	}
	
	protected void refreshExpandItem( Composite parent, int propertyID, 
								      List<String> values, boolean isResource) {
		Link link;
		CustomButton button;
		Image img;
		Control[] children = parent.getChildren();
		for (int i = 0; i < children.length; i++) {
			children[i].dispose();
		}
		
		for (String value : values) {
			link = new Link(parent, SWT.NONE );
			if (isResource)
				link.setText("<A>" + value + "</A>");
			else
				link.setText(value);
			button = new CustomButton(parent, SWT.PUSH, value, propertyID );
			button.setToolTipText("Remove");
			img  = FileManager.getImage( RDFSchemaControl.getResourceString("icon.delete2") );
			button.setImage(img);
			button.setBounds(img.getBounds());
		}
		
		img  = FileManager.getImage( RDFSchemaControl.getResourceString("icon.add") );
		
		button = new CustomButton(parent, SWT.PUSH, "", propertyID );
		button.setToolTipText("Add");
		button.setImage( img);
		button.setBounds(img.getBounds());
		
		
	}
	
	public abstract void createContent() throws Exception;
	
	public void removeItem(){
		
	}
	
	public void addItem(){
		
	}
	
	protected void checkSubclass (){
		
	}
}
